﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Globalization;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;

public partial class Private_Operator_DatePicker : System.Web.UI.UserControl
{
    string _dateFormat = "MM/dd/yyyy";
    bool _isValidDate = true;
    bool _manualInput = true;
    // bool _required = false;
    string _additionalText = String.Empty;

    public string AdditionalText
    {
        get { return _additionalText; }
        set { _additionalText = value; }
    }

    public TextBox DateTextBox
    {
        get { return DateTxt; }
    }

    public bool ManualInput
    {
        get { return _manualInput; }
        set { _manualInput = value; }
    }

    //public bool Required
    //{
    //    get { return _required; }
    //    set { _required = value; }
    //}

    public string DateFormat
    {
        get { return _dateFormat; }
        set { _dateFormat = value; }
    }

    public string SelectedDateText
    {
        get { return DateTxt.Text; }
        set { DateTxt.Text = value; }
    }

    public DateTime SelectedDate
    {
        get
        {
            DateTime result = DateTime.MinValue;

            if (!DateTime.TryParseExact(SelectedDateText, DateFormat, CultureInfo.InstalledUICulture, DateTimeStyles.None, out result))
                _isValidDate = false;
            //if (!DateTime.TryParse(SelectedDateText, out result))
            //    _isValidDate = false;

            return result;
        }
        set
        {
            DateTxt.Text = value.ToString(DateFormat);
        }
    }

    protected void Page_Init(object sender, EventArgs e)
    {
        DateTxt.Enabled = ManualInput;
        CalExtender.Format = DateFormat;
    }

    protected override void Render(HtmlTextWriter writer)
    {
        int columns = 1;

        string html = @"<table><tbody><tr>";
        writer.Write(html);
        html = String.Empty;

        if (this.AdditionalText.Length > 0)
        {
            html = @"<td>" + AdditionalText + "</td>";
            columns++;
        }

        //if (this.Required)
        //{
        //    html += @"<td width=""5px""><font color=""red"">*</font></td>";
        //    columns++;
        //}

        writer.Write(html);
        html = @"<td width=""90px"">";
        writer.Write(html);

        base.Render(writer);

        html = @"</td></tr>";
        writer.Write(html);


        if (SelectedDateText.Length > 0)
        {
            DateTime date;
            if (!DateTime.TryParseExact(SelectedDateText, DateFormat, CultureInfo.InvariantCulture, DateTimeStyles.None, out date))
                _isValidDate = false;
            //if (!DateTime.TryParse(SelectedDateText, out date))
            //    _isValidDate = false;
        }

        if (!_isValidDate && ManualInput)
        {
            html = "<tr><td style='padding-right: 18px;' align='right' colspan=" + columns + "><font style='font-size: 10pt;' color='red'>Wrong Date!</font></td></tr>";
            writer.Write(html);
        }

        html = "</tbody></table>";
        writer.Write(html);
    }
}